﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace DiracTCC.Business
{
    public class InstrumentationGenerator
    {
        public event EventHandler VideoCreated;

        public static string GetInstrumentationPath(string originalVideoPath, string argument)
        {
            var newVideoName = 
                argument
                .Replace("-", string.Empty)
                .Replace(" ", string.Empty);

            return Path.Combine(
                        Path.GetDirectoryName(originalVideoPath),
                        Path.ChangeExtension(newVideoName, ".yuv"));
        }

        public string CreateYuvVideo(string originalVideoPath, string argument, string helperAppsPath)
        {
            var prgb = new Process();
            string newPath = GetInstrumentationPath(originalVideoPath, argument);

            prgb.StartInfo.FileName = Path.Combine(helperAppsPath, "InstrumentDirac.exe");
            prgb.StartInfo.CreateNoWindow = true;
            prgb.StartInfo.Arguments = string.Format("{0} \"{1}\" \"{2}\"", argument, originalVideoPath, newPath);
            prgb.OutputDataReceived += new DataReceivedEventHandler(prgb_OutputDataReceived);
            prgb.StartInfo.RedirectStandardOutput = true;
            prgb.StartInfo.UseShellExecute = false;
            prgb.Start();
            prgb.BeginOutputReadLine();

            return newPath;
        }

        void prgb_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data) && e.Data == "Done sequence.")
            {
                if (VideoCreated != null)
                {
                    VideoCreated(this, new EventArgs());
                }
            }
        }
    }
}
